package com.tmall.testgen.client.runinspring.push.web;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class WebXmlInfo {
    
    private Map<String,String> initParam=new HashMap<String,String>();
    private List<String> listeners=new  ArrayList<String>(); ;
    private String dispatcherServletClass;
    
    
    private void putInitParam(String key,String value){
        if(initParam.get(key)==null)
            initParam.put(key, value);
    }
    public  void  initWebXmlInfo(String path){
        String content="";
        try {
            content=readFile(path);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        XStream xstream = new XStream(new DomDriver  ());
        
        xstream.alias("contextParams", ArrayList.class);
        xstream.alias("context-param", ContextParam.class);
        xstream.aliasField("param-name", ContextParam.class, "paramName");
        xstream.aliasField("param-value", ContextParam.class, "paramValue");
        
        xstream.alias("listeners", ArrayList.class);
        xstream.alias("listener", Listener.class);
        xstream.aliasField("listener-class", Listener.class, "listenerClass");
        
         
        xstream.alias("servlets", ArrayList.class);
        xstream.alias("servlet", Servlet.class);
        xstream.aliasField("servlet-name", Servlet.class,"servletName");
        xstream.aliasField("servlet-class", Servlet.class, "servletClass");
        xstream.aliasField("init-param", Servlet.class, "initParam");
        xstream.aliasField("load-on-startup", Servlet.class, "loadOnStartUp");
        
        xstream.aliasField("param-name", ContextParam.class, "paramName");
        xstream.aliasField("param-value", ContextParam.class, "paramValue");
        
        boolean haveContextParam=content.indexOf("<context-param>")>=0?true:false;
        
        if(haveContextParam){
            String contextParamContent=content.substring(content.indexOf("<context-param>"),content.lastIndexOf("</context-param>")+16);
            contextParamContent="<contextParams>"+contextParamContent+"</contextParams>";
            List<ContextParam> contextParamTem=(List<ContextParam>) xstream.fromXML(contextParamContent); 
            for(ContextParam p:contextParamTem){
                putInitParam(p.paramName.trim(), p.paramValue.trim());
            }
        }
        
        boolean haveListener=content.indexOf("<listener>")>=0?true:false;
        if(haveListener){
            String listenerContent=content.substring(content.indexOf("<listener>"),content.lastIndexOf("</listener>")+11);
            listenerContent="<listeners>"+listenerContent+"</listeners>";
            
            List<Listener> listenerTem=(List<Listener>) xstream.fromXML(listenerContent); 
              
            for(Listener l:listenerTem){
                listeners.add(l.listenerClass);
            }
        }
        
        boolean haveServlet=content.indexOf("<servlet>")>=0?true:false;
        
        if(haveServlet){
            int end=0;
            String listenerContent="";
            
            while(end>=0){
                try{
                     listenerContent+=content.substring(content.indexOf("<servlet>",end),content.indexOf("</servlet>",end)+10);
                     end=content.indexOf("</servlet>",end)+10;
                }catch(Exception e){
                    break;
                }
            }
            listenerContent="<servlets>"+listenerContent+"</servlets>";
            
            List<Servlet> servletTem=(List<Servlet>) xstream.fromXML(listenerContent); 
              
            for(Servlet s:servletTem){
                if(s.servletClass.equals("org.springframework.web.servlet.DispatcherServlet")&&s.initParam!=null){
                    putInitParam(s.initParam.paramName, s.initParam.paramValue);
                    dispatcherServletClass="org.springframework.web.servlet.DispatcherServlet";
                    break;
                }
            }
        }
        
    }
    
    

    private static String readFile(String  filePath) throws Exception{
        
        File file=new File(filePath);
        if(!file.exists()){
            throw new Exception("file is not exist!"); 
        }
            
         InputStreamReader isr=new InputStreamReader(new FileInputStream(file),"UTF-8");
        char[] temp=new char[(int) file.length()];
        isr.read(temp);
        isr.close();
        
        return new String(temp);
    }
    
    
    

    public Map<String,String>  getInitParam() {
          return initParam;
        
    }
    public List<String> getListeners() {
        return listeners;
    }

    public void setListeners(List<String> listeners) {
        this.listeners = listeners;
    }



    public String getDispatcherServletClass() {
        return dispatcherServletClass;
    }



    public void setDispatcherServletClass(String dispatcherServletClass) {
        this.dispatcherServletClass = dispatcherServletClass;
    }
    
    
    
}

 class ContextParam{
    String paramName;
    String paramValue;
    public String getParamName() {
        return paramName;
    }
    public void setParamName(String paramName) {
        this.paramName = paramName;
    }
    public String getParamValue() {
        return paramValue;
    }
    public void setParamValue(String paramValue) {
        this.paramValue = paramValue;
    }
    
}

 class Servlet{
     String servletName;
     String servletClass;
     ContextParam initParam; 
     private String loadOnStartUp;
 }
 class Listener{
    String listenerClass;

    public String getListenerClass() {
        return listenerClass;
    }

    public void setListenerClass(String listenerClass) {
        this.listenerClass = listenerClass;
    }
    
}